﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace CoreShop.App
{
    public partial class frmOrderingData : UIForm
    {
        private List<corecmsfoodordering> orderingDetail = null;
        public long orderId = 0;
        public long deskId = 0;
        private bool loadFormFlag = false;
        public frmOrderingData(long _orderId, long _deskId)
        {
            orderId = _orderId;
            deskId = _deskId;
            InitializeComponent();
        }

        private void frmfoodtask_Load(object sender, System.EventArgs e)
        {
            if (orderId > 0)
            {
                pnl_left_orderList.Visible = false;
                btnClearData.Visible = false;
                btnSelect.Visible = false;
                GetOrderingDetail();
            }
            else
            {
                GetOrderingList();
            }
            loadFormFlag = true;
        }

        private void GetOrderingList()
        {
            var orderingmasters = new corecmsfoodorderingmasterServices().QueryOrderingHangups();
            dgvOrderList.AutoGenerateColumns = false;
            this.binOrders.DataSource = orderingmasters;
            this.dgvOrderList.DataSource = this.binOrders;
            if (orderingmasters.Count > 0)
            {
                var orderingmaster = orderingmasters.First();
                orderId = orderingmaster.orderId;
                deskId  = orderingmaster.deskId;
                GetOrderingDetail();
            }
        }

        private void GetOrderingDetail()
        {
            pnlDeskName.Text = "";
            pnlCreateTime.Text = "";
            orderingDetail = new corecmsfoodorderingServices().GetListByOrderId(orderId);
            addSumRow();//添加合计行
            var orderingmaster = new corecmsfoodorderingmasterServices().GetById(orderId);
            if (orderingmaster != null)
            {
                pnlCreateTime.Text = orderingmaster.orderDate.ToString("yyyy-MM-dd HH:mm:ss");
                if (orderingmaster.userId > 0)
                {
                    var memberInfo = new corecmsuserServices().GetById(orderingmaster.userId);
                    if (memberInfo != null)
                      pnlDeskName.Text = memberInfo.mobile;
                }
            }              
            dgvGoodsList.AutoGenerateColumns = false;
            this.binDetail.DataSource = orderingDetail;
            this.dgvGoodsList.DataSource = this.binDetail;
        }


        private void addSumRow()
        {
            if ((orderingDetail == null) || (orderingDetail.Count == 0)) return;
            corecmsfoodordering orderingSum = new corecmsfoodordering();
            orderingSum.name = "合计：";
            orderingSum.quantity = 0;
            orderingSum.amount = 0;
            orderingSum.realAmount = 0;
            orderingSum.taskAmount = 0;
            orderingSum.feedAmount = 0;
            orderingSum.allAmount = 0;
            foreach (var item in orderingDetail)
            {
                item.dataTypeTitle = EnumHelper.GetDescByValue<GlobalEnumVars.OrderingDataType>(item.dataType);
                if (item.dataType != (int)GlobalEnumVars.OrderingDataType.Refund)
                {
                    orderingSum.quantity += item.quantity;
                    orderingSum.amount += item.amount;
                    orderingSum.realAmount += item.realAmount;
                    orderingSum.taskAmount += item.taskAmount;
                    orderingSum.feedAmount += item.feedAmount;
                    orderingSum.allAmount += item.allAmount;
                }
            }
            orderingDetail.Add(orderingSum);
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvOrderList_SelectionChanged(object sender, EventArgs e)
        {
            if (!loadFormFlag) return;
            if (dgvOrderList.SelectedRows.Count == 1)
            {
                orderId = CommFunc.ConvertToInt64(dgvOrderList.SelectedRows[0].Cells["orderMasterId"].Value);
            }
            else { 
                orderId = 0;
            }
            GetOrderingDetail();
        }

        private void btnClearData_Click(object sender, EventArgs e)
        {
            if (!ShowAskDialog("提示：", "确定要删除所有挂单数据吗？", UIStyle.Red)) return;
            new corecmsfoodorderingmasterServices().Delete(p => p.deskId == (int)GlobalEnumVars.OrderDeskType.OrderingHangup);
            var orderingIds = new corecmsfoodorderingmasterServices().GetList().Select(p => p.orderId).ToList();
            new corecmsfoodorderingServices().Delete(p => !orderingIds.Contains(p.orderId));
            GetOrderingList();
            ShowInfoTip("删除完成。");
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (orderId == 0)
            {
                UIMessageBox.Show("请选择一个单据后返回。", "提示:", UIStyle.Green, UIMessageBoxButtons.OK);
                return;
            }
            new corecmsfoodorderingmasterServices().Update(a => new corecmsfoodorderingmaster()
            {
                deskId = (int)GlobalEnumVars.OrderDeskType.OrderingPos,
            }, a => a.orderId == orderId);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void dgvOrderList_DoubleClick(object sender, EventArgs e)
        {
            if (orderId != 0) { btnSelect_Click(null, null); }
        }
    }
}
